package com.antoco.operation.bigscreen.controller.modelcontroller.impl;

import com.antoco.operation.bigscreen.controller.modelcontroller.intf.PaizanNumTjController;
import com.antoco.operation.bigscreen.model.dto.PaizanNumTjDTO;
import com.antoco.operation.bigscreen.model.entity.PaizanNumTj;
import com.antoco.operation.bigscreen.second.ValidList;
import com.antoco.operation.bigscreen.mapper.PaizanNumTjMapper;
import com.antoco.operation.bigscreen.service.ChannelDataService;
import com.antoco.operation.bigscreen.service.autogeneration.intf.PaizanNumTjService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/model/paizan-num-tj")
@RestController
public class PaizanNumTjControllerImpl implements PaizanNumTjController {

    @Autowired
    private PaizanNumTjService paizanNumTjService;

    @Autowired
    private PaizanNumTjMapper paizanNumTjMapper;

    @Autowired
    private ChannelDataService channelDataService;

    @Override
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public PaizanNumTjDTO save(@RequestBody PaizanNumTjDTO paizanNumTjDTO) {
        PaizanNumTj paizanNumTj = paizanNumTjMapper.asEntity(paizanNumTjDTO);
        paizanNumTjService.save(paizanNumTj);
        channelDataService.pushAllLists(this.getClass().getName());
        return paizanNumTjDTO;
    }

    @Override
    @PostMapping("/save-lists")
    @ResponseStatus(HttpStatus.CREATED)
    public List<PaizanNumTjDTO> saveLists(@Validated @RequestBody ValidList<PaizanNumTjDTO> paizanNumTjDTOLists) {
        paizanNumTjService.save(paizanNumTjMapper.asEntityList(paizanNumTjDTOLists));
        //当完成数据的操作后，调用该封装的方法获取变化后的数据进行推送
        channelDataService.pushAllLists(this.getClass().getName());
        return paizanNumTjDTOLists;
    }

    @Override
    @GetMapping("/{id}")
    public PaizanNumTjDTO findById(@PathVariable("id") String id) {
        PaizanNumTj paizanNumTj = paizanNumTjService.findById(id).orElse(null);
        return paizanNumTjMapper.asDTO(paizanNumTj);
    }

    @Override
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") String id) {
        paizanNumTjService.deleteById(id);
        channelDataService.pushAllLists(this.getClass().getName());
    }

    @Override
    @GetMapping
    public List<PaizanNumTjDTO> list() {
        return paizanNumTjMapper.asDTOList(paizanNumTjService.findAll());
    }

    @Override
    @GetMapping("/page-query")
    public Page<PaizanNumTjDTO> pageQuery(Pageable pageable) {
        Page<PaizanNumTj> paizanNumTjPage = paizanNumTjService.findAll(pageable);
        List<PaizanNumTjDTO> dtoList = paizanNumTjPage
                .stream()
                .map(paizanNumTjMapper::asDTO).collect(Collectors.toList());
        return new PageImpl<>(dtoList, pageable, paizanNumTjPage.getTotalElements());
    }

    @Override
    @PutMapping("/{id}")
    public PaizanNumTjDTO update(@RequestBody PaizanNumTjDTO paizanNumTjDTO, @PathVariable("id") String id) {
        PaizanNumTj paizanNumTj = paizanNumTjMapper.asEntity(paizanNumTjDTO);
        paizanNumTjMapper.asDTO(paizanNumTjService.update(paizanNumTj, id));
        channelDataService.pushAllLists(this.getClass().getName());
        return paizanNumTjDTO;
    }
}